﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class DimensionOperation
	{
		public DimensionOperation(UIDocument UIDoc, bool IsSprinkler, FrmDimension frm)
		{
			this.Doc = UIDoc.Document;
			this.UiDoc = UIDoc;
			this.IsSprinkler = IsSprinkler;
			this.frm = frm;
		}

		public void Execute()
		{
			this.frm.Text = (this.IsSprinkler ? "喷头间距" : "风口间距");
			this.frm.label1.Text = (this.IsSprinkler ? "喷头距轴线距离≥" : "风口距轴线距离≥");
			List<Element> list = new List<Element>();
			this.getHasElements(ref list, this.IsSprinkler);
			if (this.frm.txtDistance.Text == "" && this.frm.rbGrid.Checked)
			{
				YJKMessageBox.Information(this.frm.label1.Text.Replace("≥", "") + "不能为空！");
				return;
			}
			if (Convert.ToDouble(this.frm.txtDistance.Text) < 0.0 && this.frm.rbGrid.Checked)
			{
				YJKMessageBox.Information(this.frm.label1.Text.Replace("≥", "") + "不能小于0！");
				return;
			}
			if (this.frm.IsDisposed)
			{
				return;
			}
			List<FamilyInstance> list2;
			if (this.IsSprinkler)
			{
				list2 = (from s in list
				where s.Category.Id.IntegerValue == -2008099
				select s).Cast<FamilyInstance>().ToList<FamilyInstance>();
			}
			else
			{
				list2 = (from s in list
				where s.Category.Id.IntegerValue == -2008013
				select s).Cast<FamilyInstance>().ToList<FamilyInstance>();
			}
			if (list2.Count <= 1)
			{
				if (this.IsSprinkler)
				{
					YJKMessageBox.Information("请选择一个以上的喷头");
					return;
				}
				YJKMessageBox.Information("请选择一个以上的风口");
				return;
			}
			else
			{
				Line dimLine = this.CreateDimLine(list2);
				if (this.SetElementsDimension(list, this.frm.rbWall.Checked, dimLine, list2, NumbericTools.mmToFeet(Convert.ToDouble(this.frm.txtDistance.Text) * 1000.0)))
				{
					this.frm.SaveData();
					return;
				}
				YJKMessageBox.Information("夹角过大，无法生成标注！");
				return;
			}
		}

		private void getHasElements(ref List<Element> hasPicks, bool IsSprinkler)
		{
			CurveLoop curveLoop = this.CurveLoopGetSelCurveLoop(this.UiDoc, IsSprinkler);
			if (curveLoop == null)
			{
				return;
			}
			double viewProjectElevation = this.GetViewProjectElevation(this.Doc.ActiveView);
			YJKViewRange floorHeigh = YJKRevitTools.GetFloorHeigh(this.Doc, this.Doc.ActiveView);
			double extrusionDist = (floorHeigh.height == 0.0) ? AssistFunc.feetToMM(3300.0) : (floorHeigh.height + viewProjectElevation);
			IEnumerable<LinkedDocument> linkedDocuments = this.Doc.GetLinkedDocuments();
			List<Element> elementByCurveLoop = this.GetElementByCurveLoop(this.Doc, null, curveLoop, extrusionDist);
			hasPicks.AddRange(elementByCurveLoop);
			foreach (LinkedDocument linkedDocument in linkedDocuments)
			{
				Transform transform = Transform.CreateTranslation(linkedDocument.Transform.Origin * -1.0);
				CurveLoopIterator curveLoopIterator = CurveLoop.CreateViaCopy(curveLoop).GetCurveLoopIterator();
				List<Curve> list = new List<Curve>();
				while (curveLoopIterator.MoveNext())
				{
					Curve curve = curveLoopIterator.Current;
					curve = curve.CreateTransformed(transform);
					list.Add(curve);
				}
				CurveLoop arCurveLoop = CurveLoop.Create(list);
				hasPicks.AddRange(this.GetElementByCurveLoop(linkedDocument.Document, linkedDocument.Transform, arCurveLoop, extrusionDist));
			}
		}

		private List<Element> GetElementByCurveLoop(Document docCur, Transform tf, CurveLoop arCurveLoop, double extrusionDist)
		{
			List<Element> list = new List<Element>();
			if (arCurveLoop == null)
			{
				return list;
			}
			BoundingBoxIntersectsFilter boundingBoxIntersectsFilter = null;
			try
			{
				new List<CurveLoop>();
				XYZ xyz = new XYZ();
				XYZ xyz2 = new XYZ();
				this.getMaxMin(ref xyz, ref xyz2, extrusionDist, arCurveLoop);
				boundingBoxIntersectsFilter = new BoundingBoxIntersectsFilter(new Outline(xyz2, xyz));
			}
			catch (Exception)
			{
				return new List<Element>();
			}
			ElementFilter elementFilter = new ElementCategoryFilter(BuiltInCategory.OST_Sprinklers);
			ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_DuctTerminal);
			ElementFilter elementFilter2 = new LogicalOrFilter(elementFilter, elementCategoryFilter);
			ElementClassFilter elementClassFilter = new ElementClassFilter(typeof(Wall));
			ElementClassFilter elementClassFilter2 = new ElementClassFilter(typeof(Grid));
			LogicalOrFilter logicalOrFilter = new LogicalOrFilter(elementClassFilter, elementClassFilter2);
			LogicalOrFilter logicalOrFilter2 = new LogicalOrFilter(elementFilter2, elementClassFilter);
			if (boundingBoxIntersectsFilter == null)
			{
				return new List<Element>();
			}
			LogicalAndFilter logicalAndFilter;
			if (tf != null)
			{
				logicalAndFilter = new LogicalAndFilter(logicalOrFilter, boundingBoxIntersectsFilter);
			}
			else
			{
				logicalAndFilter = new LogicalAndFilter(logicalOrFilter2, boundingBoxIntersectsFilter);
			}
			list = new FilteredElementCollector(docCur).WherePasses(logicalAndFilter).ToElements().ToList<Element>();
			list.AddRange(this.getGridElements(docCur, arCurveLoop));
			return list;
		}

		private void getMaxMin(ref XYZ Max, ref XYZ Min, double z, CurveLoop arCurveLoop)
		{
			double minValue = double.MinValue;
			double minValue2 = double.MinValue;
			double maxValue = double.MaxValue;
			double maxValue2 = double.MaxValue;
			double maxValue3 = double.MaxValue;
			foreach (Curve curve in arCurveLoop)
			{
				this.getCurvePt(curve.GetEndPoint(1), curve.GetEndPoint(0), ref minValue, ref minValue2, ref maxValue, ref maxValue2, ref maxValue3);
			}
			Max = new XYZ(minValue, minValue2, z);
			Min = new XYZ(maxValue, maxValue2, maxValue3);
		}

		private void getCurvePt(XYZ p1, XYZ p2, ref double maxX, ref double maxY, ref double minX, ref double minY, ref double minZ)
		{
			if (p1.X > maxX)
			{
				maxX = p1.X;
			}
			if (p2.X > maxX)
			{
				maxX = p2.X;
			}
			if (p1.Y > maxY)
			{
				maxY = p1.Y;
			}
			if (p2.Y > maxY)
			{
				maxY = p2.Y;
			}
			if (p1.X < minX)
			{
				minX = p1.X;
			}
			if (p2.X < minX)
			{
				minX = p2.X;
			}
			if (p1.Y < minY)
			{
				minY = p1.Y;
			}
			if (p2.Y < minY)
			{
				minY = p2.Y;
			}
			if (p2.Z < minZ)
			{
				minZ = p2.Z;
			}
		}

		private List<Element> getGridElements(Document docCur, CurveLoop arCurveLoop)
		{
			List<Element> list = new List<Element>();
			foreach (Grid item in new FilteredElementCollector(docCur).OfClass(typeof(Grid)).ToElements().Cast<Grid>().ToList<Grid>())
			{
				if (this.inBox(arCurveLoop, item))
				{
					list.Add(item);
				}
			}
			return list;
		}

		private bool inBox(CurveLoop arCurveLoop, Grid item)
		{
			XYZ endPoint = item.Curve.GetEndPoint(0);
			XYZ endPoint2 = item.Curve.GetEndPoint(1);
			double z = endPoint.Z;
			foreach (Curve curve in arCurveLoop)
			{
				XYZ start = new XYZ(curve.GetEndPoint(0).X, curve.GetEndPoint(0).Y, z);
				XYZ end = new XYZ(curve.GetEndPoint(1).X, curve.GetEndPoint(1).Y, z);
				Line line = new YJKLine(start, end).Line;
                if ((int)item.Curve.Intersect(line) != 4)
				{
					return true;
				}
			}
			List<Curve> lines = arCurveLoop.ToList<Curve>();
			return this.IsInBox(endPoint, lines) || this.IsInBox(endPoint2, lines);
		}

		private bool IsInBox(XYZ point, List<Curve> lines)
		{
			XYZ hver = new XYZ(1.0, 0.0, 0.0);
			XYZ vver = new XYZ(0.0, 1.0, 0.0);
			List<Curve> list = (from s in lines
			where (s.GetEndPoint(0) - s.GetEndPoint(1)).Normalize().IsAlmostEqualTo(hver) || (s.GetEndPoint(0) - s.GetEndPoint(1)).Normalize().IsAlmostEqualTo(-hver)
			select s).ToList<Curve>();
			List<Curve> list2 = (from s in lines
			where (s.GetEndPoint(0) - s.GetEndPoint(1)).Normalize().IsAlmostEqualTo(vver) || (s.GetEndPoint(0) - s.GetEndPoint(1)).Normalize().IsAlmostEqualTo(-vver)
			select s).ToList<Curve>();
			return (list[0].GetEndPoint(0).X <= point.X || list[1].GetEndPoint(0).X <= point.X) && (list[0].GetEndPoint(0).X >= point.X || list[1].GetEndPoint(0).X >= point.X) && (list2[0].GetEndPoint(0).Y <= point.Y || list2[1].GetEndPoint(0).Y <= point.Y) && (list2[0].GetEndPoint(0).Y >= point.Y || list2[1].GetEndPoint(0).Y >= point.Y);
		}

		private double GetLevelToUpLevelHeight(Document doc, double dElevation)
		{
			double result = 0.0;
			Level upLevelByElevation = this.GetUpLevelByElevation(doc, dElevation);
			if (upLevelByElevation != null)
			{
				result = upLevelByElevation.Elevation - dElevation;
			}
			return result;
		}

		private Level GetUpLevelByElevation(Document doc, double dElevation)
		{
			Level result = null;
			List<Level> list = new FilteredElementCollector(doc).OfClass(typeof(Level)).Cast<Level>().ToList<Level>();
			SortedDictionary<double, Level> sortedDictionary = new SortedDictionary<double, Level>();
			foreach (Level level in list)
			{
				if (level.Elevation > dElevation)
				{
					double key = Math.Abs(level.Elevation - dElevation);
					if (!sortedDictionary.ContainsKey(key))
					{
						sortedDictionary.Add(key, level);
					}
				}
			}
			if (sortedDictionary.Count > 0)
			{
				result = sortedDictionary.First<KeyValuePair<double, Level>>().Value;
			}
			return result;
		}

		private double GetViewProjectElevation(View view)
		{
			double result = 0.0;
			Level genLevel = view.GenLevel;
			if (genLevel == null)
			{
				return result;
			}
			return genLevel.ProjectElevation;
		}

		private CurveLoop CurveLoopGetSelCurveLoop(UIDocument uiDoc, bool IsSprinkler)
		{
			CurveLoop curveLoop = new CurveLoop();
			try
			{
				ViewPlan viewPlan = uiDoc.ActiveView as Autodesk.Revit.DB.ViewPlan;
				if (viewPlan == null)
				{
					return curveLoop;
				}
				Level genLevel = viewPlan.GenLevel;
				if (genLevel == null)
				{
					return curveLoop;
				}
				string text;
				if (IsSprinkler)
				{
					text = "请框选喷头！";
				}
				else
				{
					text = "请框选风口！";
				}
				PickedBox pickedBox = uiDoc.Selection.PickBox(0, text);
				double projectElevation = genLevel.ProjectElevation;
				XYZ min = pickedBox.Min;
				XYZ max = pickedBox.Max;
				XYZ xyz = new XYZ(min.X, min.Y, projectElevation);
				XYZ xyz2 = new XYZ(min.X, max.Y, projectElevation);
				XYZ xyz3 = new XYZ(max.X, max.Y, projectElevation);
				XYZ xyz4 = new XYZ(max.X, min.Y, projectElevation);
				curveLoop.Append(Line.CreateBound(xyz, xyz2));
				curveLoop.Append(Line.CreateBound(xyz2, xyz3));
				curveLoop.Append(Line.CreateBound(xyz3, xyz4));
				curveLoop.Append(Line.CreateBound(xyz4, xyz));
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException ex)
			{
				throw ex;
			}
			catch (Exception ex2)
			{
				if (ex2.Message == "外部组件发生异常。")
				{
					throw ex2;
				}
			}
			return curveLoop;
		}

		private bool SetElementsDimension(List<Element> hasPicks, bool isWall, Line dimLine, List<FamilyInstance> Sprinklers, double length)
		{
			List<XYZ> point = new List<XYZ>();
			List<ReferenceArray> list3;
			if (isWall)
			{
				List<Wall> list = (from s in hasPicks
				where s.Category.Id.IntegerValue == -2000011
				select s).Cast<Wall>().ToList<Wall>();
				List<Wall> list2 = new List<Wall>();
				list2.AddRange(list);
				foreach (Wall wall in list)
				{
					if (((LocationCurve)wall.Location).Curve is Arc)
					{
						list2.Remove(wall);
					}
					else if (Math.Abs(((Line)((LocationCurve)wall.Location).Curve).Direction.AngleTo(dimLine.Direction) - Math.PI*.5) > 1E-06)
					{
						list2.Remove(wall);
					}
				}
				list2 = this.reSortWalls(list2, dimLine);
				if (list2.Count > 0)
				{
					list3 = this.SetReferenceArrayForWalls(Sprinklers, list2, dimLine, ref point);
				}
				else
				{
					list3 = this.SetReferenceArrayJustForInstance(Sprinklers, dimLine, ref point);
				}
			}
			else
			{
				List<Grid> list4 = (from s in hasPicks
				where s.Category.Id.IntegerValue == -2000220
				select s).Cast<Grid>().ToList<Grid>();
				List<Grid> list5 = new List<Grid>();
				list5.AddRange(list4);
				list5 = this.reSortGrids(list5, dimLine);
				foreach (Grid grid in list4)
				{
					if (Math.Abs(((Line)grid.Curve).Direction.AngleTo(dimLine.Direction) - Math.PI*.5) > 1E-06)
					{
						list5.Remove(grid);
					}
				}
				if (list5.Count > 0)
				{
					list3 = this.SetReferenceArrayForGrids(Sprinklers, list5, dimLine, length, ref point);
				}
				else
				{
					list3 = this.SetReferenceArrayJustForInstance(Sprinklers, dimLine, ref point);
				}
			}
			if (list3 == null || list3.Count == 0)
			{
				return false;
			}
			using (XDimensionJig xdimensionJig = new XDimensionJig(this.UiDoc, dimLine, point))
			{
				XYZ xyz = xdimensionJig.PickDimensionPoint("请选择标注点！");
				DimensionType dimTypeByName = this.GetDimTypeByName();
				Parameter parameter = dimTypeByName.GetParameter(BuiltInParameter.DIM_WITNS_LINE_CNTRL);
				if (parameter != null && !parameter.IsReadOnly)
				{
					parameter.Set(1);
				}
				foreach (ReferenceArray referenceArray in list3)
				{
					if (referenceArray.Size > 1)
					{
						try
						{
							Dimension dimension = this.Doc.Create.NewDimension(this.Doc.ActiveView, Line.CreateUnbound(dimLine.Origin + xyz, dimLine.Direction), referenceArray, dimTypeByName);
							this.Doc.Regenerate();
							dimension.Location.Move(new XYZ(1.0, 0.0, 0.0));
							dimension.Location.Move(new XYZ(-1.0, 0.0, 0.0));
						}
						catch (Exception)
						{
							continue;
						}
						DimensionOperation.mouse_event(2048, 0, 0, 1, 0);
					}
				}
			}
			return true;
		}

		[DllImport("user32.dll")]
		private static extern void mouse_event(int flags, int dX, int dY, int buttons, int extraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private List<Grid> reSortGrids(List<Grid> grids, Line dimLine)
		{
			Dictionary<XYZ, Grid> dictionary = new Dictionary<XYZ, Grid>();
			List<Grid> list = new List<Grid>();
			using (List<Grid>.Enumerator enumerator = grids.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					Grid item = enumerator.Current;
					if (!(from s in dictionary
					where s.Key.IsAlmostEqualTo(this.getGridXYZ(item, dimLine))
					select s).Any<KeyValuePair<XYZ, Grid>>())
					{
						dictionary.Add(this.getGridXYZ(item, dimLine), item);
					}
				}
			}
			if (dimLine.Direction.X != 0.0)
			{
				dictionary = (from s in dictionary
				orderby s.Key.X
				select s).ToDictionary((KeyValuePair<XYZ, Grid> s) => s.Key, (KeyValuePair<XYZ, Grid> s) => s.Value);
			}
			else
			{
				dictionary = (from s in dictionary
				orderby s.Key.Y
				select s).ToDictionary((KeyValuePair<XYZ, Grid> s) => s.Key, (KeyValuePair<XYZ, Grid> s) => s.Value);
			}
			foreach (KeyValuePair<XYZ, Grid> keyValuePair in dictionary)
			{
				list.Add(keyValuePair.Value);
			}
			return list;
		}

		private List<Wall> reSortWalls(List<Wall> walls, Line dimLine)
		{
			Dictionary<XYZ, Wall> dictionary = new Dictionary<XYZ, Wall>();
			List<Wall> list = new List<Wall>();
			foreach (Wall wall in walls)
			{
				XYZ wallPt = this.getwallXYZ(wall, dimLine);
				if (!(from s in dictionary
				where s.Key.IsAlmostEqualTo(wallPt)
				select s).Any<KeyValuePair<XYZ, Wall>>())
				{
					dictionary.Add(wallPt, wall);
				}
			}
			if (dimLine.Direction.X != 0.0)
			{
				dictionary = (from s in dictionary
				orderby s.Key.X
				select s).ToDictionary((KeyValuePair<XYZ, Wall> s) => s.Key, (KeyValuePair<XYZ, Wall> s) => s.Value);
			}
			else
			{
				dictionary = (from s in dictionary
				orderby s.Key.Y
				select s).ToDictionary((KeyValuePair<XYZ, Wall> s) => s.Key, (KeyValuePair<XYZ, Wall> s) => s.Value);
			}
			foreach (KeyValuePair<XYZ, Wall> keyValuePair in dictionary)
			{
				list.Add(keyValuePair.Value);
			}
			return list;
		}

		private XYZ getwallXYZ(Wall item, Line dimLine)
		{
			return dimLine.Project(((LocationCurve)item.Location).Curve.GetEndPoint(0)).XYZPoint;
		}

		private XYZ getGridXYZ(Grid item, Line dimLine)
		{
			return dimLine.Project(item.Curve.GetEndPoint(0)).XYZPoint;
		}

		private XYZ getPointXYZ(Element item, Line dimLine)
		{
			return dimLine.Project(((LocationPoint)item.Location).Point).XYZPoint;
		}

		private List<ReferenceArray> SetReferenceArrayJustForInstance(List<FamilyInstance> Sprinklers, Line dimLine, ref List<XYZ> Points)
		{
			List<XYZ> list = new List<XYZ>();
			List<ReferenceArray> list2 = new List<ReferenceArray>();
			ReferenceArray referenceArray = new ReferenceArray();
			using (List<FamilyInstance>.Enumerator enumerator = Sprinklers.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					Element item = enumerator.Current;
					if (!(from s in list
					where Math.Abs(s.X - this.getPointXYZ(item, dimLine).X) < 0.001 && Math.Abs(s.Y - this.getPointXYZ(item, dimLine).Y) < 0.001
					select s).Any<XYZ>())
					{
						list.Add(this.getPointXYZ(item, dimLine));
						Reference referenceOfDetailComponent = this.GetReferenceOfDetailComponent(item, dimLine, this.Doc.ActiveView);
						if (referenceOfDetailComponent == null)
						{
							list2.Clear();
							Points.Clear();
							return list2;
						}
						referenceArray.Append(referenceOfDetailComponent);
						Points.Add(((LocationPoint)item.Location).Point);
					}
					else
					{
						Points.Add(((LocationPoint)item.Location).Point);
					}
				}
			}
			list2.Add(referenceArray);
			return list2;
		}

		private List<ReferenceArray> SetReferenceArrayForGrids(List<FamilyInstance> Sprinklers, List<Grid> Grids, Line dimLine, double length, ref List<XYZ> Points)
		{
			List<ReferenceArray> list = new List<ReferenceArray>();
			int num = 1;
			Grid grid = null;
			XYZ gridsDirection = this.getGridsDirection(Grids, dimLine);
			foreach (Grid grid2 in Grids)
			{
				if (num == 1)
				{
					list.Add(this.getFirstGridRef(Sprinklers, grid2, dimLine, gridsDirection, length, ref Points));
				}
				if (num == Grids.Count)
				{
					if (grid != null)
					{
						list.Add(this.getMidGridRef(Sprinklers, grid2, grid, dimLine, gridsDirection, length, ref Points));
					}
					list.Add(this.getLastGridRef(Sprinklers, grid2, dimLine, gridsDirection, length, ref Points));
				}
				if (num != 1 && num != Grids.Count)
				{
					list.Add(this.getMidGridRef(Sprinklers, grid2, grid, dimLine, gridsDirection, length, ref Points));
				}
				grid = grid2;
				num++;
			}
			return list;
		}

		private ReferenceArray getFirstGridRef(List<FamilyInstance> Sprinklers, Grid item, Line dimLine, XYZ direction, double length, ref List<XYZ> Points)
		{
			bool flag = true;
			ReferenceArray referenceArray = new ReferenceArray();
			List<XYZ> list = new List<XYZ>();
			using (List<FamilyInstance>.Enumerator enumerator = Sprinklers.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					FamilyInstance sprinkler = enumerator.Current;
					if ((-direction).IsAlmostEqualTo((dimLine.Project(((LocationPoint)sprinkler.Location).Point).XYZPoint - dimLine.Project(this.getLinkPoint(this.Doc, item, item.Curve.GetEndPoint(0))).XYZPoint).Normalize()))
					{
						if (!(from s in list
						where Math.Abs(s.X - this.getPointXYZ(sprinkler, dimLine).X) < 0.001 && Math.Abs(s.Y - this.getPointXYZ(sprinkler, dimLine).Y) < 0.001
						select s).Any<XYZ>())
						{
							list.Add(this.getPointXYZ(sprinkler, dimLine));
							if (dimLine.Project(((LocationPoint)sprinkler.Location).Point).XYZPoint.DistanceTo(dimLine.Project(this.getLinkPoint(this.Doc, item, item.Curve.GetEndPoint(0))).XYZPoint) < length)
							{
								flag = false;
							}
							referenceArray.Append(this.GetReferenceOfDetailComponent(sprinkler, dimLine, this.UiDoc.ActiveView));
							Points.Add(((LocationPoint)sprinkler.Location).Point);
						}
						else
						{
							if (dimLine.Project(((LocationPoint)sprinkler.Location).Point).XYZPoint.DistanceTo(dimLine.Project(this.getLinkPoint(this.Doc, item, item.Curve.GetEndPoint(0))).XYZPoint) < length)
							{
								flag = false;
							}
							Points.Add(((LocationPoint)sprinkler.Location).Point);
						}
					}
				}
			}
			if (flag)
			{
				referenceArray.Append(this.GetGridReferenceOfDetailComponent(item, this.UiDoc.ActiveView, dimLine));
				Points.Add(this.getLinkPoint(this.Doc, item, item.Curve.GetEndPoint(0)));
			}
			return referenceArray;
		}

		private XYZ getLinkPoint(Document doc, Element ele, XYZ point)
		{
			if (ele.Document.IsLinked)
			{
				return doc.GetLinkedDocument(ele.Id).GetPointInLinkDocument(point);
			}
			return point;
		}

		private ReferenceArray getMidGridRef(List<FamilyInstance> Sprinklers, Grid item, Grid lastItem, Line dimLine, XYZ direction, double length, ref List<XYZ> Points)
		{
			bool flag = true;
			bool flag2 = true;
			ReferenceArray referenceArray = new ReferenceArray();
			XYZ xyzpoint = dimLine.Project(this.getLinkPoint(this.Doc, item, item.Curve.GetEndPoint(0))).XYZPoint;
			XYZ xyzpoint2 = dimLine.Project(this.getLinkPoint(this.Doc, lastItem, lastItem.Curve.GetEndPoint(0))).XYZPoint;
			List<XYZ> list = new List<XYZ>();
			using (List<FamilyInstance>.Enumerator enumerator = Sprinklers.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					FamilyInstance sprinkler = enumerator.Current;
					XYZ xyzpoint3 = dimLine.Project(((LocationPoint)sprinkler.Location).Point).XYZPoint;
					if (!(from s in list
					where Math.Abs(s.X - this.getPointXYZ(sprinkler, dimLine).X) < 0.001 && Math.Abs(s.Y - this.getPointXYZ(sprinkler, dimLine).Y) < 0.001
					select s).Any<XYZ>())
					{
						list.Add(this.getPointXYZ(sprinkler, dimLine));
						if (Math.Abs(direction.X) > 1E-05)
						{
							if (xyzpoint3.X > ((xyzpoint.X > xyzpoint2.X) ? xyzpoint2.X : xyzpoint.X) && xyzpoint3.X < ((xyzpoint.X > xyzpoint2.X) ? xyzpoint.X : xyzpoint2.X))
							{
								if (xyzpoint.DistanceTo(xyzpoint3) < length)
								{
									flag = false;
								}
								if (xyzpoint2.DistanceTo(xyzpoint3) < length)
								{
									flag2 = false;
								}
								Points.Add(((LocationPoint)sprinkler.Location).Point);
								referenceArray.Append(this.GetReferenceOfDetailComponent(sprinkler, dimLine, this.UiDoc.ActiveView));
							}
						}
						else if (xyzpoint3.Y > ((xyzpoint.Y > xyzpoint2.Y) ? xyzpoint2.Y : xyzpoint.Y) && xyzpoint3.Y < ((xyzpoint.Y > xyzpoint2.Y) ? xyzpoint.Y : xyzpoint2.Y))
						{
							if (xyzpoint.DistanceTo(xyzpoint3) < length)
							{
								flag = false;
							}
							if (xyzpoint2.DistanceTo(xyzpoint3) < length)
							{
								flag2 = false;
							}
							Points.Add(((LocationPoint)sprinkler.Location).Point);
							referenceArray.Append(this.GetReferenceOfDetailComponent(sprinkler, dimLine, this.UiDoc.ActiveView));
						}
					}
					else
					{
						Points.Add(((LocationPoint)sprinkler.Location).Point);
					}
				}
			}
			if (flag)
			{
				referenceArray.Append(this.GetGridReferenceOfDetailComponent(item, this.UiDoc.ActiveView, dimLine));
				Points.Add(this.getLinkPoint(this.Doc, item, item.Curve.GetEndPoint(0)));
			}
			if (flag2)
			{
				referenceArray.Append(this.GetGridReferenceOfDetailComponent(lastItem, this.UiDoc.ActiveView, dimLine));
				Points.Add(this.getLinkPoint(this.Doc, lastItem, lastItem.Curve.GetEndPoint(0)));
			}
			return referenceArray;
		}

		private ReferenceArray getLastGridRef(List<FamilyInstance> Sprinklers, Grid item, Line dimLine, XYZ direction, double length, ref List<XYZ> Points)
		{
			bool flag = true;
			ReferenceArray referenceArray = new ReferenceArray();
			List<XYZ> list = new List<XYZ>();
			using (List<FamilyInstance>.Enumerator enumerator = Sprinklers.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					FamilyInstance sprinkler = enumerator.Current;
					if (direction.IsAlmostEqualTo((dimLine.Project(((LocationPoint)sprinkler.Location).Point).XYZPoint - dimLine.Project(this.getLinkPoint(this.Doc, item, item.Curve.GetEndPoint(0))).XYZPoint).Normalize()))
					{
						if (!(from s in list
						where Math.Abs(s.X - this.getPointXYZ(sprinkler, dimLine).X) < 0.001 && Math.Abs(s.Y - this.getPointXYZ(sprinkler, dimLine).Y) < 0.001
						select s).Any<XYZ>())
						{
							list.Add(this.getPointXYZ(sprinkler, dimLine));
							if (dimLine.Project(((LocationPoint)sprinkler.Location).Point).XYZPoint.DistanceTo(dimLine.Project(this.getLinkPoint(this.Doc, item, item.Curve.GetEndPoint(0))).XYZPoint) < length)
							{
								flag = false;
							}
							referenceArray.Append(this.GetReferenceOfDetailComponent(sprinkler, dimLine, this.UiDoc.ActiveView));
							Points.Add(((LocationPoint)sprinkler.Location).Point);
						}
						else
						{
							if (dimLine.Project(((LocationPoint)sprinkler.Location).Point).XYZPoint.DistanceTo(dimLine.Project(this.getLinkPoint(this.Doc, item, item.Curve.GetEndPoint(0))).XYZPoint) < length)
							{
								flag = false;
							}
							Points.Add(((LocationPoint)sprinkler.Location).Point);
						}
					}
				}
			}
			if (flag)
			{
				referenceArray.Append(this.GetGridReferenceOfDetailComponent(item, this.UiDoc.ActiveView, dimLine));
				Points.Add(this.getLinkPoint(this.Doc, item, item.Curve.GetEndPoint(0)));
			}
			return referenceArray;
		}

		private List<ReferenceArray> SetReferenceArrayForWalls(List<FamilyInstance> Sprinklers, List<Wall> Walls, Line dimLine, ref List<XYZ> Points)
		{
			List<ReferenceArray> list = new List<ReferenceArray>();
			int num = 1;
			Wall wall = null;
			XYZ wallsDirection = this.getWallsDirection(Walls, dimLine);
			foreach (Wall wall2 in Walls)
			{
				if (num == 1)
				{
					list.Add(this.getFirstWallRef(Sprinklers, wall2, dimLine, wallsDirection, ref Points));
				}
				if (num == Walls.Count)
				{
					if (wall != null)
					{
						list.Add(this.getMidWallRef(Sprinklers, wall2, wall, dimLine, wallsDirection, ref Points));
					}
					list.Add(this.getLastWallRef(Sprinklers, wall2, dimLine, wallsDirection, ref Points));
				}
				if (num != 1 && num != Walls.Count)
				{
					list.Add(this.getMidWallRef(Sprinklers, wall2, wall, dimLine, wallsDirection, ref Points));
				}
				wall = wall2;
				num++;
			}
			return list;
		}

		private ReferenceArray getFirstWallRef(List<FamilyInstance> Sprinklers, Wall item, Line dimLine, XYZ direction, ref List<XYZ> Points)
		{
			ReferenceArray referenceArray = new ReferenceArray();
			List<XYZ> list = new List<XYZ>();
			using (List<FamilyInstance>.Enumerator enumerator = Sprinklers.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					FamilyInstance sprinkler = enumerator.Current;
					if ((-direction).IsAlmostEqualTo((dimLine.Project(((LocationPoint)sprinkler.Location).Point).XYZPoint - dimLine.Project(this.getLinkPoint(this.Doc, item, ((LocationCurve)item.Location).Curve.GetEndPoint(0))).XYZPoint).Normalize()))
					{
						if (!(from s in list
						where Math.Abs(s.X - this.getPointXYZ(sprinkler, dimLine).X) < 0.001 && Math.Abs(s.Y - this.getPointXYZ(sprinkler, dimLine).Y) < 0.001
						select s).Any<XYZ>())
						{
							list.Add(this.getPointXYZ(sprinkler, dimLine));
							referenceArray.Append(this.GetReferenceOfDetailComponent(sprinkler, dimLine, this.UiDoc.ActiveView));
							Points.Add(((LocationPoint)sprinkler.Location).Point);
						}
						else
						{
							Points.Add(((LocationPoint)sprinkler.Location).Point);
						}
					}
				}
			}
			XYZ xyz = new XYZ();
			referenceArray.Append(this.GetWallReferenceOfDetailComponent(item, this.UiDoc.ActiveView, -direction, ref xyz, dimLine));
			if (Points.Count != 0 && !xyz.IsZeroLength())
			{
				Points.Add(xyz);
			}
			return referenceArray;
		}

		private ReferenceArray getMidWallRef(List<FamilyInstance> Sprinklers, Wall item, Wall lastWall, Line dimLine, XYZ direction, ref List<XYZ> Points)
		{
			ReferenceArray referenceArray = new ReferenceArray();
			XYZ xyzpoint = dimLine.Project(this.getLinkPoint(this.Doc, item, ((LocationCurve)item.Location).Curve.GetEndPoint(0))).XYZPoint;
			XYZ xyzpoint2 = dimLine.Project(this.getLinkPoint(this.Doc, lastWall, ((LocationCurve)lastWall.Location).Curve.GetEndPoint(0))).XYZPoint;
			List<XYZ> list = new List<XYZ>();
			using (List<FamilyInstance>.Enumerator enumerator = Sprinklers.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					FamilyInstance sprinkler = enumerator.Current;
					if (!(from s in list
					where Math.Abs(s.X - this.getPointXYZ(sprinkler, dimLine).X) < 0.001 && Math.Abs(s.Y - this.getPointXYZ(sprinkler, dimLine).Y) < 0.001
					select s).Any<XYZ>())
					{
						list.Add(this.getPointXYZ(sprinkler, dimLine));
						XYZ xyzpoint3 = dimLine.Project(((LocationPoint)sprinkler.Location).Point).XYZPoint;
						if (Math.Abs(direction.X) > 0.0001)
						{
							if (xyzpoint3.X > ((xyzpoint.X > xyzpoint2.X) ? xyzpoint2.X : xyzpoint.X) && xyzpoint3.X < ((xyzpoint.X > xyzpoint2.X) ? xyzpoint.X : xyzpoint2.X))
							{
								referenceArray.Append(this.GetReferenceOfDetailComponent(sprinkler, dimLine, this.UiDoc.ActiveView));
								Points.Add(((LocationPoint)sprinkler.Location).Point);
							}
						}
						else if (xyzpoint3.Y > ((xyzpoint.Y > xyzpoint2.Y) ? xyzpoint2.Y : xyzpoint.Y) && xyzpoint3.Y < ((xyzpoint.Y > xyzpoint2.Y) ? xyzpoint.Y : xyzpoint2.Y))
						{
							referenceArray.Append(this.GetReferenceOfDetailComponent(sprinkler, dimLine, this.UiDoc.ActiveView));
							Points.Add(((LocationPoint)sprinkler.Location).Point);
						}
					}
					else
					{
						Points.Add(((LocationPoint)sprinkler.Location).Point);
					}
				}
			}
			XYZ xyz = new XYZ();
			referenceArray.Append(this.GetWallReferenceOfDetailComponent(lastWall, this.UiDoc.ActiveView, direction, ref xyz, dimLine));
			if (!xyz.IsZeroLength())
			{
				Points.Add(xyz);
			}
			referenceArray.Append(this.GetWallReferenceOfDetailComponent(item, this.UiDoc.ActiveView, -direction, ref xyz, dimLine));
			if (!xyz.IsZeroLength())
			{
				Points.Add(xyz);
			}
			return referenceArray;
		}

		private ReferenceArray getLastWallRef(List<FamilyInstance> Sprinklers, Wall item, Line dimLine, XYZ direction, ref List<XYZ> Points)
		{
			ReferenceArray referenceArray = new ReferenceArray();
			List<XYZ> list = new List<XYZ>();
			List<XYZ> list2 = new List<XYZ>();
			using (List<FamilyInstance>.Enumerator enumerator = Sprinklers.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					FamilyInstance sprinkler = enumerator.Current;
					if (direction.IsAlmostEqualTo((dimLine.Project(((LocationPoint)sprinkler.Location).Point).XYZPoint - dimLine.Project(this.getLinkPoint(this.Doc, item, ((LocationCurve)item.Location).Curve.GetEndPoint(0))).XYZPoint).Normalize()))
					{
						if (!(from s in list2
						where Math.Abs(s.X - this.getPointXYZ(sprinkler, dimLine).X) < 0.001 && Math.Abs(s.Y - this.getPointXYZ(sprinkler, dimLine).Y) < 0.001
						select s).Any<XYZ>())
						{
							list2.Add(this.getPointXYZ(sprinkler, dimLine));
							referenceArray.Append(this.GetReferenceOfDetailComponent(sprinkler, dimLine, this.UiDoc.ActiveView));
							list.Add(((LocationPoint)sprinkler.Location).Point);
						}
						else
						{
							Points.Add(((LocationPoint)sprinkler.Location).Point);
						}
					}
				}
			}
			XYZ xyz = new XYZ();
			referenceArray.Append(this.GetWallReferenceOfDetailComponent(item, this.UiDoc.ActiveView, direction, ref xyz, dimLine));
			if (list.Count != 0 && !xyz.IsZeroLength())
			{
				list.Add(xyz);
				Points.AddRange(list);
			}
			return referenceArray;
		}

		private XYZ getGridsDirection(List<Grid> Grids, Line dimLine)
		{
			XYZ result = dimLine.Direction;
			if (Grids.Count > 1)
			{
				result = (dimLine.Project(Grids.Last<Grid>().Curve.GetEndPoint(0)).XYZPoint - dimLine.Project(Grids.FirstOrDefault<Grid>().Curve.GetEndPoint(0)).XYZPoint).Normalize();
			}
			return result;
		}

		private XYZ getWallsDirection(List<Wall> Walls, Line dimLine)
		{
			XYZ result = dimLine.Direction;
			if (Walls.Count > 1)
			{
				result = (dimLine.Project(((LocationCurve)Walls.Last<Wall>().Location).Curve.GetEndPoint(0)).XYZPoint - dimLine.Project(((LocationCurve)Walls.FirstOrDefault<Wall>().Location).Curve.GetEndPoint(0)).XYZPoint).Normalize();
			}
			return result;
		}

		private Line CreateDimLine(List<FamilyInstance> hasPicks)
		{
			Line line = this.IsOnOneLine(hasPicks);
			if (line == null)
			{
				if (this.IsSprinkler)
				{
					YJKMessageBox.Information("请框选二个喷头确定标注方向");
				}
				else
				{
					YJKMessageBox.Information("请框选二个风口确定标注方向");
				}
				line = this.PickDirectionForDimLine();
			}
			return line;
		}

		private Line PickDirectionForDimLine()
		{
			List<FamilyInstance> list;
			for (;;)
			{
				ISelectionFilter selectionFilter = new SprinklerOrAirTerminalFilter(this.IsSprinkler);
				list = this.UiDoc.Selection.PickElementsByRectangle(selectionFilter, "请选择两个实例").Cast<FamilyInstance>().ToList<FamilyInstance>();
				if (list.Count<FamilyInstance>() == 2)
				{
					break;
				}
				YJKMessageBox.Information("请选择两个实例，以确定方向！");
			}
			XYZ xyz = this.ChangePicksInstanceToPoint(list).FirstOrDefault<XYZ>();
			XYZ xyz2 = this.ChangePicksInstanceToPoint(list).Last<XYZ>();
			return Line.CreateUnbound(xyz, xyz2 - xyz);
		}

		private Line IsOnOneLine(List<FamilyInstance> hasPicks)
		{
			List<XYZ> hasPicks2 = this.ChangePicksInstanceToPoint(hasPicks);
			if (this.OnX(hasPicks2) != null)
			{
				return this.OnX(hasPicks2);
			}
			if (this.OnY(hasPicks2) != null)
			{
				return this.OnY(hasPicks2);
			}
			if (this.OnTwoPoint(hasPicks2) != null)
			{
				return this.OnTwoPoint(hasPicks2);
			}
			return null;
		}

		private List<XYZ> ChangePicksInstanceToPoint(List<FamilyInstance> hasPicks)
		{
			List<XYZ> list = new List<XYZ>();
			double z = ((LocationPoint)hasPicks.First<FamilyInstance>().Location).Point.Z;
			foreach (FamilyInstance familyInstance in hasPicks)
			{
				XYZ point = ((LocationPoint)familyInstance.Location).Point;
				list.Add(new XYZ(point.X, point.Y, z));
			}
			return list;
		}

		private Line OnX(List<XYZ> hasPicks)
		{
			XYZ xyz = hasPicks.FirstOrDefault<XYZ>();
			Line line = Line.CreateUnbound(xyz, new XYZ(1.0, 0.0, 0.0));
			foreach (XYZ xyz2 in hasPicks)
			{
				if (line.Distance(xyz2) > this.DeviationLength)
				{
					return null;
				}
			}
			return Line.CreateUnbound(xyz, new XYZ(1.0, 0.0, 0.0));
		}

		private Line OnY(List<XYZ> hasPicks)
		{
			XYZ xyz = hasPicks.FirstOrDefault<XYZ>();
			Line line = Line.CreateUnbound(xyz, new XYZ(0.0, 1.0, 0.0));
			foreach (XYZ xyz2 in hasPicks)
			{
				if (line.Distance(xyz2) > this.DeviationLength)
				{
					return null;
				}
			}
			return Line.CreateUnbound(xyz, new XYZ(0.0, 1.0, 0.0));
		}

		private Line OnTwoPoint(List<XYZ> hasPicks)
		{
			XYZ xyz = hasPicks.FirstOrDefault<XYZ>();
			XYZ xyz2 = hasPicks.Last<XYZ>();
			Line line = Line.CreateUnbound(xyz, xyz2 - xyz);
			foreach (XYZ xyz3 in hasPicks)
			{
				if (line.Distance(xyz3) > this.DeviationLength)
				{
					return null;
				}
			}
			return Line.CreateUnbound(xyz, xyz2 - xyz);
		}

        private Reference GetReferenceOfDetailComponent(Element element, Line dimLine, Autodesk.Revit.DB.View view)
		{
			Options options = new Options();
			options.ComputeReferences = true;
			options.IncludeNonVisibleObjects = true;
			if (view != null)
			{
				options.View = view;
			}
			else
			{
				options.DetailLevel = (ViewDetailLevel)3;
			}
			foreach (GeometryObject geometryObject in element.get_Geometry(options))
			{
				if (geometryObject is Point)
				{
					return ((Point)geometryObject).Reference;
				}
				if (geometryObject is GeometryInstance)
				{
					foreach (GeometryObject geometryObject2 in ((GeometryInstance)geometryObject).SymbolGeometry)
					{
						if (geometryObject2 is Line && Math.Abs(((GeometryInstance)geometryObject).Transform.OfVector(((Line)geometryObject2).Direction).AngleTo(dimLine.Direction) - Math.PI*.5) < 0.001)
						{
							return ((Line)geometryObject2).Reference;
						}
					}
				}
			}
			return null;
		}

		private Reference GetWallReferenceOfDetailComponent(Wall element, View view, XYZ Normal, ref XYZ point, Line dimLine)
		{
			foreach (GeometryObject geometryObject in element.get_Geometry(new Options
			{
				ComputeReferences = true,
				IncludeNonVisibleObjects = true,
                DetailLevel = (ViewDetailLevel)3
			}))
			{
				if (geometryObject is Solid && ((Solid)geometryObject).Faces.Size >= 6)
				{
					foreach (object obj in ((Solid)geometryObject).Faces)
					{
						PlanarFace planarFace = (PlanarFace)obj;
						if (RevitVersionFuncs.GetFaceFaceNormal(planarFace).IsAlmostEqualTo(Normal))
						{
							if (element.Document.IsLinked)
							{
								XYZ linkPoint = this.getLinkPoint(this.Doc, element, planarFace.Origin);
								point = linkPoint;
								XYZ xyzpoint = dimLine.Project(linkPoint).XYZPoint;
								XYZ xyz = new XYZ(xyzpoint.X, xyzpoint.Y, linkPoint.Z);
								XYZ xyz2 = new XYZ(xyzpoint.X, xyzpoint.Y, linkPoint.Z) + (new XYZ(xyzpoint.X, xyzpoint.Y, linkPoint.Z) - linkPoint).Normalize() * 0.05;
								XYZ xyz3 = new XYZ(xyzpoint.X, xyzpoint.Y, linkPoint.Z + 1.0);
								return RevitVersionFuncs.GetReference(view.Document.Create.NewReferencePlane2(xyz, xyz2, xyz3, view));
							}
							point = planarFace.Origin;
							return planarFace.Reference;
						}
					}
				}
			}
			return null;
		}

		private Reference GetGridReferenceOfDetailComponent(Grid element, View view, Line dimLine)
		{
			Options options = new Options();
			options.ComputeReferences = true;
			options.IncludeNonVisibleObjects = true;
			if (view != null)
			{
				options.View = view;
			}
			else
			{
				options.DetailLevel = (ViewDetailLevel)3;
			}
			GeometryElement geometryElement = element.get_Geometry(options);
			if (element.Document.IsLinked)
			{
				XYZ xyz = this.getLinkPoint(this.Doc, element, element.Curve.GetEndPoint(0)) + new XYZ(0.0, 0.0, dimLine.Origin.Z);
				XYZ linkPoint = this.getLinkPoint(this.Doc, element, element.Curve.GetEndPoint(1));
				XYZ xyz2 = new XYZ(dimLine.Project(linkPoint).XYZPoint.X, dimLine.Project(linkPoint).XYZPoint.Y, xyz.Z);
				XYZ xyz3 = xyz2;
				XYZ xyz4 = xyz2 + (xyz - xyz2).Normalize() * 0.01;
				XYZ xyz5 = new XYZ(xyz2.X, xyz2.Y, xyz2.Z + 1.0);
				ReferencePlane rPlane = view.Document.Create.NewReferencePlane2(xyz3, xyz4, xyz5, view);
				this.Doc.Regenerate();
				return RevitVersionFuncs.GetReference(rPlane);
			}
			foreach (GeometryObject geometryObject in geometryElement)
			{
				if (geometryObject is Line)
				{
					return ((Line)geometryObject).Reference;
				}
			}
			return null;
		}

		private DimensionType GetDimTypeByName()
		{
			int scale = this.Doc.ActiveView.Scale;
			string text = "HY-3.5-长仿宋-0.8(下左)";
			string strMark = "对角线 1.0mm";
			DimensionType dimensionType = null;
			foreach (Element element in new FilteredElementCollector(this.Doc).OfClass(typeof(DimensionType)).ToElements())
			{
				if (element.Name == text)
				{
					dimensionType = (element as DimensionType);
					break;
				}
			}
			if (dimensionType == null)
			{
				dimensionType = this.CopyDimensionType(text, strMark);
			}
			return dimensionType;
		}

		private DimensionType CopyDimensionType(string strLabel, string strMark)
		{
			DimensionType result = null;
			string versionNumber = RevitVersion.GetVersionNumber(this.Doc);
			string text = Path.Combine(Product.FamilyLocation, versionNumber, "Template", "HYBIMSpace给排水样板.rte");
			Document document = null;
			if (!File.Exists(text))
			{
				return null;
			}
			document = this.Doc.Application.OpenDocumentFile(text);
			Element element = null;
			foreach (Element element2 in new FilteredElementCollector(document).OfClass(typeof(DimensionType)).ToElements())
			{
				if (element2.Name == strLabel)
				{
					element = element2;
					break;
				}
			}
			Element element3 = null;
            foreach (Element element4 in new FilteredElementCollector(document).OfClass(typeof(Autodesk.Revit.DB.ElementType)).ToElements())
			{
				if (element4.Name == strMark)
				{
					element3 = element4;
					break;
				}
			}
			if (element == null || element3 == null)
			{
				return null;
			}
			List<ElementId> list = new List<ElementId>();
			list.Add(element.Id);
			List<ElementId> list2 = new List<ElementId>();
			list2.Add(element3.Id);
			SubTransaction subTransaction = new SubTransaction(this.Doc);
			subTransaction.Start();
			try
			{
				ICollection<ElementId> collection = ElementTransformUtils.CopyElements(document, list, this.Doc, Transform.Identity, new CopyPasteOptions());
				ElementTransformUtils.CopyElements(document, list2, this.Doc, Transform.Identity, new CopyPasteOptions());
				subTransaction.Commit();
				if (collection.Count > 0)
				{
					result = (this.Doc.GetElement(collection.ElementAt(0)) as DimensionType);
				}
			}
			catch
			{
				subTransaction.RollBack();
			}
			document.Close();
			return result;
		}

		private List<DimensionType> GetDimensionTypes(Document doc)
		{
			List<DimensionType> list = new List<DimensionType>();
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(DimensionType)).ToElements())
			{
				DimensionType item = element as DimensionType;
				list.Add(item);
			}
			return list;
		}

		private Document Doc;

		private UIDocument UiDoc;

		private bool IsSprinkler = true;

		private FrmDimension frm;

		private double DeviationLength = NumbericTools.mmToFeet(200.0);
	}
}
